package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author: Eric
 * @since: 2020/12/1
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    /**
     * 登陆成功后，进入main.html页面时获取登陆用户名
     * @return
     */
    @GetMapping("/getLoginUsername")
    public Result getLoginUsername() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
    }

    @GetMapping("/findMenuByUsername")
    public Result findMenuByUsername(@RequestParam("username") String loginUsername) {
        List<Menu> menus = userService.findMenuByUsername(loginUsername);
        return new Result(true, MessageConstant.GET_MENU_SUCCESS, menus);
    }

    /**
     * 用户列表的分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("findPage")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult<com.itheima.health.pojo.User> userPageResult = userService.findByPage(queryPageBean);
        return new Result(true, "查询用户列表成功", userPageResult);
    }

    /**
     * 用户信息的回显
     * @param id
     * @return
     */
    @GetMapping("findById")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findById(int id) {
        com.itheima.health.pojo.User user = userService.findById(id);
        return new Result(true, "查询用户信息成功", user);
    }

    /**
     * 与用户关联的角色信息回显
     * @param id
     * @return
     */
    @GetMapping("findRoleIdsByUserId")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result findRoleIdsByUserId(int id) {
        List<Integer> roleIds = userService.findRoleIdsByUserId(id);
        return new Result(true, "查询角色成功", roleIds);
    }

    /**
     * 修改用户信息
     * @param user
     * @param roleIds
     * @return
     */
    @PostMapping("update")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    public Result update(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds) {
        userService.update(user, roleIds);
        return new Result(true, "更新成功");
    }

    /**
     * 添加用户
     * @param user
     * @param roleIds
     * @return
     */
    @PostMapping("add")
    @PreAuthorize("hasAuthority('USER_ADD')")
    public Result add(@RequestBody com.itheima.health.pojo.User user, Integer[] roleIds) {
        userService.add(user, roleIds);
        return new Result(true, "添加成功");
    }

    /**
     * 删除用户（逻辑删除）
     * @param id
     * @return
     */
    @PostMapping("deleteById")
    @PreAuthorize("hasAuthority('USER_DELETE')")
    public Result deleteById(int id) {
        userService.deleteById(id);
        return new Result(true, "删除成功");
    }

    /**
     * 修改用户密码
     * @param map
     * @return
     */
    @PostMapping("updatePassword")
    public Result updatePassword(@RequestBody Map<String, Object> map,int id) {
        if (map.get("firstNewPassword") == null || map.get("secondNewPassword") == null) {
            return new Result(false, "输入的密码不能为空");
        }
        if (!map.get("firstNewPassword").equals(map.get("secondNewPassword"))) {
            return new Result(false, "两次输入的密码不一样，请重新输入");
        }
        userService.updatePassword(map,id);
        return new Result(true, "修改成功");
    }

    /**
     * 重置用户密码
     * @param id
     * @return
     */
    @PostMapping("resetById")
    public Result resetById(int id) {
        userService.resetById(id);
        return new Result(true, "重置用户密码成功");
    }
}